package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.mapper.ItemTagMapper;
import com.ddwl.goods.model.ItemTag;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2021/4/7 11:28
 */
@Repository
public class ItemTagDao extends TkDao<ItemTagMapper, ItemTag> {

    @Resource
    private ItemTagMapper itemTagMapper;

    public List<ItemTag> findItemTagByItemIdsAndCodes(List<String> itemIds, List<String> codes) {
        Example example = getExample();
        example.and().andIn("itemId", itemIds)
                .andIn("code", codes);
        return itemTagMapper.selectByExample(example);
    }

    public void deleteByItemIdAndCode(String itemId, String code) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId)
                .andEqualTo("code", code);
        ItemTag itemTag = new ItemTag();
        itemTag.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        itemTag.setGmtModified(Instant.now());
        itemTagMapper.updateByExampleSelective(itemTag,example);
    }

    public ItemTag getByItemIdAndCode(String itemId, String code) {
        ItemTag itemTag = new ItemTag();
        itemTag.setItemId(itemId);
        itemTag.setCode(code);
        return this.selectOne(itemTag);
    }

    public List<ItemTag> findByItemIds(List<String> itemIds) {
        Example example = getExample();
        example.and().andIn("itemId", itemIds);
        return itemTagMapper.selectByExample(example);
    }

    public void deleteByItemIdsAndCode(List<String> itemIds,String code) {
        Example example = getExample();
        example.and().andIn("itemId", itemIds)
                .andEqualTo("code", code);
        ItemTag itemTag = new ItemTag();
        itemTag.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        itemTagMapper.updateByExampleSelective(itemTag,example);
    }

    public void restoreByItemIdsAndCode(List<String> itemIds,String code) {
        Example example = getExample();
        example.and().andIn("itemId", itemIds)
                .andEqualTo("code", code);
        ItemTag itemTag = new ItemTag();
        itemTag.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        itemTagMapper.updateByExampleSelective(itemTag,example);
    }

    public List<ItemTag> getByItemId(String itemId) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId);
        return itemTagMapper.selectByExample(example);
    }

    public void deleteByItemId(String itemId) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId);
        ItemTag it = new ItemTag();
        it.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        it.setGmtModified(Instant.now());
        itemTagMapper.updateByExampleSelective(it, example);
    }
}
